/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "相机控制.h"


static float32	g视点距离 = {};
static vec3		g视点偏移 = {};
static vec3		g眼睛位置 = {};
static vec3		g视点 = {};

static vec3		gX平移方向 = {};
static vec3		gY平移方向 = {};



void f_set相机旋转(S_摄像机* camera, const vec3& rot) {
	camera->f_set累加旋转(-rot.y*0.2, rot.x*0.2, rot.z);
}

void f_set相机距离(S_摄像机* camera, const float32 dis) {
	camera->f_set缩放(dis, dis, dis);
}

void f_set相机平移(S_摄像机* camera, const vec2& move) {
	vec3 横向移动X = vec3{ 1, 0, 0 } * move.x;
	vec3 横向移动Y = vec3{ 0, 1, 0 } * move.y;

	if (camera->m_投影模式 == E_投影模式::e_透视) {
		camera->m_眼睛偏移 = g视点偏移 + 横向移动X + 横向移动Y;
	}
	else {
		camera->m_眼睛偏移 = g视点偏移 - (横向移动X + 横向移动Y);
	}

	//std::cout << "A 眼睛偏移 = " << S_Core::g_相机->m_眼睛偏移.x << " : " << S_Core::g_相机->m_眼睛偏移.y << " : " << S_Core::g_相机->m_眼睛偏移.z << std::endl;
	camera->m_isUpdate = true;
}

void f_camera_开始相机变换(S_摄像机* camera) {
	g视点距离 = camera->m_视点距离;
	g视点偏移 = camera->m_眼睛偏移;
	g视点 = camera->m_视点;

	g眼睛位置 = camera->f_get位置();

	if (g视点偏移.x > 1000000000) {
		g视点偏移.x = 1000000000;
	}
	if (g视点偏移.x < -1000000000) {
		g视点偏移.x = -1000000000;
	}
}

S_VecSphere f_camera_get视线方向和距离(S_摄像机* camera) {
	S_VecSphere s;

	auto rx = RotationMatrix(M_角度转弧度(camera->m_变换.rotation.x), { 1.0f,0.0f,0.0f });
	auto ry = RotationMatrix(M_角度转弧度(-camera->m_变换.rotation.y), { 0.0f,1.0f,0.0f });
	auto sel = ScaleMatrix({ 1.0f, -1.0f, 1.0f });

	s.pos = vec_normalize2(ry * rx * sel * vec3{ 0, 0, 1 });
	//s.pos = vec_normalize2((S_Core::g_相机->m_相机位置矩阵) * vec3 { 0, 0, 1 });
	s.radius = fabs(camera->m_视点距离);
	return s;
}



void f_camera_结束相机变换() {
	g视点距离 = 0;
	g视点偏移 = {};
	g视点 = {};
}



vec3 f_camera_getX平行方向() {
	//gX平移方向 = 
	//return vec3();
	return {};
}

vec3 f_camera_get眼睛位置() {
	return { -g眼睛位置.x, -g眼睛位置.y, -g眼睛位置.z };
}

vec3 f_camera_get视点全局位置() {
	return g视点 + g视点偏移;
}





